package flexexamples.repository
{
	import flexexamples.domain.Iteration;
	import flexexamples.domain.Project;
	import flexexamples.domain.UserStory;
	import flexexamples.repository.impl.ProjectRepositoryRemoteObject;
	
	import flexunit.framework.TestCase;
	
	import mx.collections.ArrayCollection;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.remoting.RemoteObject;
	

	/**
	 * @author p900009 Ole C. Langfjæran (BEKK)
	 */
	public class ProjectRepositoryRemoteObjectTest extends TestCase
	{
		private var service : RemoteObject = getService();
		
		private var repo:ProjectRepositoryRemoteObject;
		
		private var serverWait:int = 3000;
		
		override public function setUp() : void
		{
			repo = new ProjectRepositoryRemoteObject();
			repo.service = service;
			repo.getDataAsEvent = true;
		}
		
		/*.saveProject() */
		public function testSaveProject() : void
		{
			var story:UserStory = new UserStory();
			story.title = "Drag and drop and forget";
			
			var iteration:Iteration = new Iteration();
			iteration.addUserStory(story);
			
			var project:Project = new Project();
			project.name = "Tom Kapinos Project";
			project.addIteration(iteration);
			
			
			repo.saveProject(project, addAsync(handleResultSaveProject, serverWait), handleFault);
		}
		
		private function handleResultSaveProject( data:Object ) : void
		{
			assertNotNull(data);
			assertTrue(data.result is Project);
		}
		
		/*.getAllProject() */
		public function testGetAllProjects() : void
		{
			repo.getAllProjects(addAsync(handleResultGetAllProjects, serverWait), handleFault);
		}
		
		private function handleResultGetAllProjects( data:Object ) : void
		{
			assertNotNull(data);
			assertTrue(data.result is ArrayCollection);
		}
		
		/*
		* Fault Handler...
		*/
		private function handleFault( info:Object, passThroughData:Object=null ) : void
		{
			//This will resolve as an error because of the serverwait
			trace("Received fault from Server: "+info);
		}
		
		/*
		* Helper methods
		*/
		
		private function getService() : RemoteObject
		{
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.addChannel(new AMFChannel(null, "http://localhost:8080/spring-hibernate-blazeds-war/messagebroker/amf"));
			
			var remoteService:RemoteObject = new RemoteObject("easyScrumRemote");
			remoteService.channelSet = channelSet;
			
			return remoteService;
		}
	}
}